<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryFormRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="app名称" prop="appName">
        <el-input
          v-model="queryParams.appName"
          placeholder="请输入app名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="appKey" prop="appKey">
        <el-input
          v-model="queryParams.appKey"
          placeholder="请输入appKey"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['content:appInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="small"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['content:appInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['content:appInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          @click="handleExport"
          v-hasPermi="['content:appInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="appInfoList"
      :default-sort="tableSort"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="app名称" align="center" prop="appName" />
      <el-table-column label="公司" align="center" prop="companyName" />
      <el-table-column label="appKey" align="center" prop="appKey" />
      <el-table-column label="appSecret" align="center" prop="appSecret" />
      <el-table-column label="微信appid" align="center" prop="wxAppid" />
      <!-- <el-table-column label="微信secret" align="center" prop="wxSecret" /> -->
      <!-- <el-table-column label="app url" align="center" prop="appUrl" />
      <el-table-column label="简介" align="center" prop="appDesc" />
      <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" sortable="custom">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['content:appInfo:edit']"
          >详情</el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['content:appInfo:edit']"
          >修改</el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['content:appInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page="queryParams.pageNum"
      :limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改app配置对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px" append-to-body  >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司" prop="companyId">
          <el-select 
          v-model="form.companyId" 
          placeholder="请选择公司"
          filterable
          remote
          reserve-keyword
          :remote-method="handleRemoteCompanyOptions"
          :loading="remoteLoading"
          >
            <el-option
                v-for="item in companyOptions"
                :key="item.id"
                :value="item.id"
                :label="item.companyName"
              />
          </el-select>
        </el-form-item>
        <el-form-item label="app名称" prop="appName">
          <el-input v-model="form.appName" placeholder="请输入app名称" />
        </el-form-item>
        <el-form-item label="appKey" prop="appKey">
          <el-input v-model="form.appKey" placeholder="请输入appKey" />
        </el-form-item>
        <el-form-item label="appSecret" prop="appSecret">
          <el-input v-model="form.appSecret" placeholder="请输入appSecret" />
        </el-form-item>
        <el-form-item label="微信appid" prop="wxAppid">
          <el-input v-model="form.wxAppid" placeholder="请输入微信appid" />
        </el-form-item>
        <el-form-item label="微信secret" prop="wxSecret">
          <el-input v-model="form.wxSecret" placeholder="请输入微信secret" />
        </el-form-item>
        <el-form-item label="app url" prop="appUrl">
          <el-input v-model="form.appUrl" placeholder="请输入app url" />
        </el-form-item>
        <el-form-item label="简介" prop="appDesc">
          <el-input v-model="form.appDesc" placeholder="请输入简介" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
          <div class="dialog-footer">
            <el-button @click="handleDialogCancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
          </div>
      </template>
    </el-dialog>
    <!-- 添加或修改app配置对话框 end -->



  </div>
</template>

<script setup name="AppInfo">
import { ref, toRefs } from "vue";
import { listAppInfo, getAppInfo, delAppInfo, addAppInfo, updateAppInfo, exportXlsUrl } from "@/api/content/appInfo";
import {getCompanySelectList} from "@/api/content/company"

const { proxy } = getCurrentInstance();

  // 遮罩层
  const loading = ref(true);
  // 选中数组
  const selectedIds = ref([]);
  // 非单个禁用
  const single = ref(true);
  // 非多个禁用
  const multiple = ref(true);
  // 显示搜索条件
  const showSearch = ref(true);
  // 总条数
  const total = ref(0);
  // app配置表格数据
  const appInfoList  = ref([]);
  // 弹出层标题
  const dialogTitle  = ref("");
  // 是否显示弹出层
  const dialogVisible  = ref(false);
  const detailDialogVisible  = ref(false);

const remoteLoading = ref(false);
const companyOptions = ref([]);

  // 查询参数
     const reactiveData  = reactive(
     {
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          companyId: null,
          companyName: null,
          appName: null,
          appKey: null,
          appSecret: null,
          wxAppid: null,
          wxSecret: null,
          appUrl: null,
          appDesc: null,
          params: {
            sort: 'createTime',
            order: 'descending',
          }
        },
        form: {},
        rules: {
        companyId: [
          { required: true, message: "公司不能为空", trigger: "blur" }
        ],
        appName: [
          { required: true, message: "app名称不能为空", trigger: "blur" }
        ],
        yn: [
          { required: true, message: "0无效1有效不能为空", trigger: "blur" }
        ],
        tenantId: [
          { required: true, message: "租户编号不能为空", trigger: "blur" }
        ]
        }
     });

      const {queryParams,form,rules} = toRefs(reactiveData);

      const tableSort = computed(()=>{
        if(queryParams.value.params.sort){
          return {
            prop: queryParams.value.params.sort,
            order: queryParams.value.params.order
          }
        }
        return null;
      })

    /** 查询app配置列表 */
   function getList() {
     loading.value = true;
      listAppInfo(queryParams.value).then(response => {
        appInfoList.value = response.data.records;
        total.value = response.data.total;
        loading.value = false;
      });
    }

  function handleRemoteCompanyOptions(query){
    if(query){
      remoteLoading.value = true;
    getCompanySelectList(query).then(res=>{
      remoteLoading.value = false;
      companyOptions.value = res.data;
    })
    }else{
      companyOptions.value = [];
      remoteLoading.value = false;
    }
    
  }

    // 取消按钮
    function handleDialogCancel() {
      dialogVisible.value = false;
      reset();
    }
    // 表单重置
    function reset() {
      form.value = {
        id: null,
        companyId: null,
        companyName: null,
        appName: null,
        appKey: null,
        appSecret: null,
        wxAppid: null,
        wxSecret: null,
        appUrl: null,
        appDesc: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        yn: null,
        tenantId: null
      };
      proxy.resetForm("formRef");
    }
    /** 搜索按钮操作 */
   function handleQuery() {
      queryParams.value.pageNum = 1;
      getList();
    }
    /** 重置按钮操作 */
    function resetQuery() {
      proxy.resetForm("queryFormRef");
      handleQuery();
    }

    // 多选框选中数据
    function handleSelectionChange(selection) {
      selectedIds.value = selection.map(item => item.id)
      single.value = selection.length!==1
      multiple.value = !selection.length
    }

    // 排序
    function handleSortChange({ column, prop, order }){
      queryParams.value.params.sort = prop?prop:null;
      queryParams.value.params.order = order?order:null;
      getList();
    }

    /** 新增按钮操作 */
    function handleAdd() {
      reset();
      dialogVisible.value = true;
      dialogTitle.value = "添加app配置";
    }

    /** 详情按钮操作 */
    function handleDetail(row) {
      reset();
      const id = row.id || selectedIds.value
      getAppInfo(id).then(response => {
        form.value = response.data;
        detailDialogVisible.value = true;
      });
    }

    /** 修改按钮操作 */
    function handleUpdate(row) {
      reset();
      const id = row.id || selectedIds.value
      getAppInfo(id).then(response => {
        form.value = response.data;
        dialogVisible.value = true;
        dialogTitle.value = "修改app配置";
        companyOptions.value = [{
          id: form.value.companyId,
          companyName: form.value.companyName
        }]
      });
    }
    /** 提交按钮 */
    function submitForm() {
      proxy.$refs["formRef"].validate(valid => {
        if (valid) {
          if (form.value.id != null) {
            updateAppInfo(form.value).then(response => {
              proxy.$modal.msgSuccess("修改成功");
              dialogVisible.value = false;
              getList();
            });
          } else {
            addAppInfo(form.value).then(response => {
              proxy.$modal.msgSuccess("新增成功");
              dialogVisible.value = false;
              getList();
            });
          }
        }
      });
    }
    /** 删除按钮操作 */
   function handleDelete(row) {
      const delids = row.id || selectedIds.value;
      proxy.$modal.confirm('是否确认删除app配置编号为"' + delids + '"的数据项？').then(function() {
        return delAppInfo(delids);
      }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
   /** 导出按钮操作 */
   function handleExport() {
      proxy.download(exportXlsUrl, {
        ...queryParams.value
      }, `appInfo_${new Date().getTime()}.xlsx`)
    }
   /** 初始化数据 **/
   getList();
</script>
